// pages/venue/share/index.js
import Poster from '../../../components/poster/poster';
const app = getApp();
const ajax = app.myRequest()
import base from '../../../utils/base64src'
var posterConfig = {
        width: 750,
        height: 1334,
        backgroundColor: '#fff',
        debug: false,
        pixelRatio: 1,
        preload:'true',
        hideLoading:'true',
        texts: [
            {
                x: 50,
                y: 1000,
                baseLine: 'top',
                text: 'ewre2w',
                fontSize: 32,
                color: '#929292',
            },
            {
              x: 50,
              y: 1050,
              baseLine: 'top',
              text: 'ewreqrq',
              fontSize: 32,
              color: '#929292',
            },
            {
              x: 50,
              y: 1150,
              width:350,
              baseLine: 'middle',
              lineNum:4,
              lineHeight:50,
              text: 'erwqw',
              fontSize: 26,
              color: '#929292',
            }
        ],
        images: [
            {
                width: 650,
                height: 750,
                x: 50,
                y: 50,
                borderRadius: 40,
                url: 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3921496974,3733365713&fm=26&gp=0.jpg',
            },
            {
              width: 150,
              height: 150,
              x: 500,
              y: 1100,
              url: '/images/back.png',
            }
        ]
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    posterConfig:posterConfig,
    huizhi:''

  },
  
  onPosterSuccess(e) {
    console.log('111')
    wx.showToast({
      title: '加载中...',
      mask: true,
      icon: 'loading'
      })
    console.log(e)
    const { detail } = e;
    // this.setData({img:detail})
    wx.hideLoading();
    wx.previewImage({
        current: detail,
        urls: [detail],
    })
    wx.saveImageToPhotosAlbum({
      filePath: detail,
      success(res) {
        console.log(res.errMsg)
        if(res.errMsg){ 
          wx.showToast({  
            title: '图片保存成功',  
            icon: 'success',  
            duration: 3000  
          })  
        }
        
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
},
  

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.preservation(options) // 保存信息
    this.getWellKnownSaying() // 获取名人名言
    this.getCode() // 获取二维码
    // this.onPosterSuccess()
    this.drawImage()
  },

  // 名人名言
  getWellKnownSaying(){
    var _this = this
    ajax.getRequest('/content/inspirational').then((res)=>{
      console.log(posterConfig.texts[2].text)
      posterConfig.texts[2].text = res.data.data
    })
  },

  // 保存场馆信息
  preservation(options){
    console.log(options)
    var id = options.id
    var latitude = options.latitude
    var longitude = options.longitude
    this.setData({ id , latitude , longitude })
    posterConfig.texts[0].text = options.name
    posterConfig.texts[1].text = '地址:'+options.address
  },

  // 生成海报
  drawImage(){
    console.log('res')
    this.setData({ posterConfig }, () => {      
      Poster.create(true);    // 入参：true为抹掉重新生成
  });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getCode(){
    var page = 'pages/venue/venueHome/index'
    var id = this.data.id
    var latitude = Number(this.data.latitude).toFixed(4) // 保留4位小数点
    var longitude = Number(this.data.longitude).toFixed(4) // 保留4位小数点

    var scene = String(id) +'&'+ String(latitude) +'&'+ String(longitude)
    console.log(scene)
    var width = 300
    var _this = this
    ajax.postRequest('/Content/qrcode',{page,scene,width,is_hyaline:true}).then((res)=>{
      console.log(res,'res')
      const arrayBuffer =  base(res.data.data)
      var img = ''
      arrayBuffer.then((res)=>{
        _this.setData({
          img : res
        })
        posterConfig.images[1].url = res
      })
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})